package com.changgou.business.controller;


import com.changgou.business.pojo.Seconds;
import com.changgou.business.service.SecondsService;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//tb_seconds表的增删改查
@RestController
@CrossOrigin
@RequestMapping("/second")
public class SecondsController {

    @Autowired
    private SecondsService secondsService ;

    /**
     * 秒杀商品信息更新
     * @param seconds
     * @return
     */
    @GetMapping(value = "/add")
    public Result secondsAdd(@RequestBody Seconds seconds){
        Integer index = secondsService.add(seconds);
        if (index>0) {
            return new Result(true, StatusCode.OK,"秒杀商品信息更新成功");
        }

        return new Result(false, StatusCode.ERROR,"秒杀商品信息更新失败");
    }

    /**
     * 根据时间段查找商品列表
     * @param id
     * @return
     */
    @GetMapping("/findSeckill_goods/{id}/{page}/{size}")
    public Result findSeckill_goods(@PathVariable Integer id,@PathVariable Integer page,@PathVariable Integer size){
        PageResult<Seconds> list = secondsService.findGoodsBySid(id,page,size);
        return new Result(true, StatusCode.OK,"秒杀商品列表查询成功",list);
    }

    /**
     * 添加秒杀商品列表查询（条件查询）
     * @param goodsName
     * @return
     */
    @GetMapping("/findSeckillGoodsByName/{goodsName}/{page}/{size}")
    public Result findSeckillGoodsByName(@PathVariable String goodsName,@PathVariable Integer page,@PathVariable Integer size){
        PageResult<Seconds> pr = secondsService.findSeckillGoodsByName(goodsName,page,size);
        return new Result(true, StatusCode.OK,"查询成功",pr);
    }

    /**
     * 删除活动跟商品的关联
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteId(@PathVariable("id") Integer id){
        secondsService.deleteId(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}

